<!-- 
说明：订单表表单
作者：292303959@qq.com
日期：2018-10-17 10:5:39
版本：1.0.0
-->
<template>
  <el-dialog :title="title"
             append-to-body
             visible
             lock-scroll
             :before-close="handleClose"
             width="700px">
    <el-form ref="form"
             :model="form"
             :label-width="ui.formLabelWidth"
             :element-loading-text="ui.loadingText"
             v-loading="ui.busying">
      <el-row type="flex">
        <el-col :span="24">
          <mk-form-item label="物流公司"
                        prop="logisticsname"
                        :mk-le-length="32"
                        required>
            <el-select v-model="form.logisticsname">
              <el-option v-for="(item,index) in logList"
                         :key="index"
                         :value="item"
                         :label="item.lcvalue"></el-option>
            </el-select>
          </mk-form-item>
        </el-col>
        <el-col :span="24">
          <mk-form-item label="发货单号"
                        prop="mname"
                        :mk-le-length="32"
                        required>
            <el-input v-model="form.logisticsnumber"></el-input>
          </mk-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <mk-button @click="handleClose">取消</mk-button>
      <mk-button @click="onSubmit"
                 :type="2">{{btnName}}</mk-button>
    </div>
  </el-dialog>
</template>

<script>
import { MkBase } from 'element-ui'
import formData from './sendform-data'
export default {
  name: 't-order-form',
  mixins: [MkBase],
  props: {
    title: String,
    btnName: String,
    row: Object
  },
  async mounted () {
    // if (this.isAdd === false) {
    //   const form = cloneDeep(this.row)
    //   this.form = form
    // }
    this.form.orderid = this.row.orderid
    this.ui.busying = false
    await this.loadLogList()
  },
  data () {
    const form = formData()
    return {
      ui: {
        formLabelWidth: '100px',
        busying: true,
        loadingText: '正在加载'
      },
      form,
      logList: []
    }
  },
  methods: {
    async valid () {
      if (!this.validate) {
        let fn = this.$refs.form.validate
        let nodejs = false
        this.validate = this.$promise({ fn, nodejs })
      }
      return await this.validate()
    },
    async onSubmit () {
      if (await this.valid()) {
        this.ui.loadingText = '正在保存'
        this.ui.busying = true
        try {
          let data = await this.$post('/tOrder/sendOrder.json', {tOrderLogistics: this.form})
          if (data) {
            this.$emit('close', true)
          }
        } finally {
          this.ui.busying = false
        }
      }
    },
    handleClose () {
      this.$emit('close')
    },
    async loadLogList () {
      const res = await this.$get('query.json', {
        sqlCode: 'cpLogisticsConfig.select_list'
      })
      this.logList = res.list
    }
  }
}
</script>
